EC-CUBE 2.4.4
[ class tree: EC-CUBE 2.4.4 ] [ index: EC-CUBE 2.4.4 ] [ all elements ]

Source for file SC_SendMail.php

Documentation is available at SC_SendMail.php

  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.lockon.co.jp/
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  */
  23.  
  24. require_once(dirname(__FILE__'/../module/Mail.php');
  25. require_once(dirname(__FILE__'/../module/Mail/mime.php');
  26.  
  27. //--- テキスト/HTML メール送信
  28. class SC_SendMail {
  29.  
  30.     var $to;            //  送信先
  31.     var $subject;       //  題名
  32.     var $body;          //  本文
  33.     var $cc;            // CC
  34.     var $bcc;           // BCC
  35.     var $replay_to;     // replay_to
  36.     var $return_path;   // return_path
  37.     var $arrEncode;
  38.     var $objMailMime;
  39.     var $arrTEXTEncode;
  40.     var $arrHTMLEncode;
  41.     var $objMail;
  42.  
  43.     // コンストラクタ
  44.     function SC_SendMail({
  45.         $this->arrRecip array();
  46.         $this->to = "";
  47.         $this->subject = "";
  48.         $this->body = "";
  49.         $this->cc = "";
  50.         $this->bcc = "";
  51.         $this->replay_to = "";
  52.         $this->return_path = "";
  53.         $this->arrEncode = array();
  54.         $this->backend MAIL_BACKEND;
  55.         $this->host SMTP_HOST;
  56.         $this->port SMTP_PORT;
  57.         $this->objMailMime = new Mail_mime();
  58.         mb_language"Japanese" );
  59.  
  60.         //-- PEAR::Mailを使ってメール送信オブジェクト作成
  61.         $this->objMail =Mail::factory($this->backend,
  62.                                         $this->getBackendParams($this->backend));
  63.     }
  64.  
  65.     // 送信先の設定
  66.     function setRecip($key$recipient{
  67.         $this->arrRecip[$key$recipient;
  68.     }
  69.     
  70.     // 宛先の設定
  71.     function setTo($to$to_name ""{
  72.         if($to != ""{
  73.             $this->to = $this->getNameAddress($to_name$to);
  74.             $this->setRecip("To"$to);
  75.         }
  76.     }
  77.  
  78.     // 送信元の設定
  79.     function setFrom($from$from_name ""{
  80.         $this->from $this->getNameAddress($from_name$from);
  81.     }
  82.  
  83.     // CCの設定
  84.     function setCc($cc$cc_name ""{
  85.         if($cc != ""{
  86.             $this->cc = $this->getNameAddress($cc_name$cc);
  87.             $this->setRecip("Cc"$cc);
  88.         }
  89.     }
  90.  
  91.     // BCCの設定
  92.     function setBCc($bcc{
  93.         if($bcc != ""{
  94.             $this->bcc = $bcc;
  95.             $this->setRecip("Bcc"$bcc);
  96.         }
  97.     }
  98.  
  99.     // Reply-Toの設定
  100.     function setReplyTo($reply_to{
  101.         if($reply_to != ""{
  102.             $this->reply_to $reply_to;
  103.         }
  104.     }
  105.  
  106.     // Return-Pathの設定
  107.     function setReturnPath($return_path{
  108.         $this->return_path = $return_path;
  109.     }
  110.  
  111.     // 件名の設定
  112.     function setSubject($subject{
  113.         $this->subject = mb_encode_mimeheader($subject"JIS"'B'"\n");
  114.         $this->subject = str_replace("\x0D\x0A""\n"$this->subject);
  115.         $this->subject = str_replace("\x0D""\n"$this->subject);
  116.         $this->subject = str_replace("\x0A""\n"$this->subject);
  117.     }
  118.  
  119.     // 本文の設定
  120.     function setBody($body{
  121.         $this->body = mb_convert_encoding($body"JIS"CHAR_CODE);
  122.     }
  123.  
  124.     // SMTPサーバの設定
  125.     function setHost($host{
  126.         $this->host $host;
  127.         $arrHost array(
  128.                 'host' => $this->host,
  129.                 'port' => $this->port
  130.         );
  131.         //-- PEAR::Mailを使ってメール送信オブジェクト作成
  132.         $this->objMail =Mail::factory("smtp"$arrHost);
  133.  
  134.     }
  135.  
  136.     // SMTPポートの設定
  137.     function setPort($port{
  138.         $this->port $port;
  139.         $arrHost array(
  140.                 'host' => $this->host,
  141.                 'port' => $this->port
  142.         );
  143.         //-- PEAR::Mailを使ってメール送信オブジェクト作成
  144.         $this->objMail =Mail::factory("smtp"$arrHost);
  145.     }
  146.  
  147.     // 名前<メールアドレス>の形式を生成
  148.     function getNameAddress($name$mail_address{
  149.             if($name != ""{
  150.                 // 制御文字を変換する。
  151.                 $_name $name;
  152.                 $_name ereg_replace("<","<"$_name);
  153.                 $_name ereg_replace(">",">"$_name);
  154.                 if(OS_TYPE != 'WIN'{
  155.                     // windowsでは文字化けするので使用しない。
  156.                     // $_name = mb_convert_encoding($_name,"JIS",CHAR_CODE);
  157.                 }
  158.                 $_name mb_encode_mimeheader($_name"JIS"'B'"\n");
  159.                 $name_address "\""$_name "\"<" $mail_address ">";
  160.             else {
  161.                 $name_address $mail_address;
  162.             }
  163.             return $name_address;
  164.     }
  165.  
  166.     function setItem($to$subject$body$fromaddress$from_name$reply_to=""$return_path=""$errors_to=""$bcc=""$cc =""{
  167.         $this->setBase($to$subject$body$fromaddress$from_name$reply_to$return_path$errors_to$bcc$cc);
  168.     }
  169.  
  170.     function setItemHtml($to$subject$body$fromaddress$from_name$reply_to=""$return_path=""$errors_to=""$bcc=""$cc =""{
  171.         $this->setBase($to$subject$body$fromaddress$from_name$reply_to$return_path$errors_to$bcc$cc);
  172.     }
  173.  
  174.     /*  ヘッダ等を格納
  175.          $to            -> 送信先メールアドレス
  176.          $subject       -> メールのタイトル
  177.          $body          -> メール本文
  178.          $fromaddress   -> 送信元のメールアドレス
  179.          $header        -> ヘッダー
  180.          $from_name     -> 送信元の名前(全角OK)
  181.          $reply_to      -> reply_to設定
  182.          $return_path   -> return-pathアドレス設定(エラーメール返送用)
  183.          $cc            -> カーボンコピー
  184.          $bcc           -> ブラインドカーボンコピー
  185.     */
  186.     function setBase($to$subject$body$fromaddress$from_name$reply_to=""$return_path=""$errors_to=""$bcc=""$cc =""{
  187.         // 宛先設定
  188.         $this->setTo($to);
  189.         // 件名設定
  190.         $this->setSubject($subject);
  191.         // 本文設定(iso-2022-jpだと特殊文字が?で送信されるのでJISを使用する)
  192.         $this->setBody($body);
  193.         // 送信元設定
  194.         $this->setFrom($fromaddress$from_name);
  195.         // 返信先設定
  196.         $this->setReplyTo($reply_to);
  197.         // CC設定
  198.         $this->setCc($cc);
  199.         // BCC設定
  200.         $this->setBcc($bcc);
  201.  
  202.         // Errors-Toは、ほとんどのSMTPで無視され、Return-Pathが優先されるためReturn_Pathに設定する。
  203.         if($errors_to != ""{
  204.             $this->return_path = $errors_to;
  205.         else if($return_path != ""{
  206.             $this->return_path = $return_path;
  207.         else {
  208.             $this->return_path = $fromaddress;
  209.         }
  210.     }
  211.  
  212.     // ヘッダーを返す
  213.     function getBaseHeader({
  214.         //-- 送信するメールの内容と送信先
  215.         $arrHeader['MIME-Version''1.0';
  216.         $arrHeader['To'$this->to;
  217.         $arrHeader['Subject'$this->subject;
  218.         $arrHeader['From'$this->from;
  219.         $arrHeader['Return-Path'$this->return_path;
  220.         if($this->reply_to != ""{
  221.             $arrHeader['Reply-To'$this->reply_to;
  222.         }
  223.         if($this->cc != ""{
  224.             $arrHeader['Cc'$this->cc;
  225.         }
  226.         if($this->bcc != ""{
  227.             $arrHeader['Bcc'$this->bcc;
  228.         }
  229.         $arrHeader['Date'date("D, j M Y H:i:s O");
  230.         return $arrHeader;
  231.     }
  232.  
  233.     // ヘッダーを返す
  234.     function getTEXTHeader({
  235.         $arrHeader $this->getBaseHeader();
  236.         $arrHeader['Content-Type'"text/plain; charset=\"ISO-2022-JP\"";
  237.         $arrHeader['Content-Transfer-Encoding'"7bit";
  238.         return $arrHeader;
  239.     }
  240.  
  241.     // ヘッダーを返す
  242.     function getHTMLHeader({
  243.         $arrHeader $this->getBaseHeader();
  244.         $arrHeader['Content-Type'"text/html; charset=\"ISO-2022-JP\"";
  245.         $arrHeader['Content-Transfer-Encoding'"ISO-2022-JP";
  246.         return $arrHeader;
  247.     }
  248.     
  249.     // 送信先を返す
  250.     function getRecip({
  251.         switch ($this->backend{
  252.         case "mail":
  253.             return $this->to;
  254.             break;
  255.         case "sendmail":
  256.         case "smtp":
  257.         default:
  258.             return $this->arrRecip;
  259.             break;
  260.         }
  261.     }
  262.  
  263.     /**
  264.      * TXTメール送信を実行する.
  265.      *
  266.      * 設定された情報を利用して, メールを送信する.
  267.      * メールの宛先に Cc や Bcc が設定されていた場合は, 宛先ごとに複数回送信を行う.
  268.      *
  269.      * - getRecip() 関数の返り値が配列の場合は, 返り値のメールアドレスごとに,
  270.      *   PEAR::Mail::send() 関数を実行する.
  271.      * - getRecip() 関数の返り値が string の場合は, 返り値のメールアドレスを
  272.      *   RCPT TO: に設定し, PEAR::Mail::send() 関数を実行する.
  273.      *
  274.      * @return void 
  275.      */
  276.     function sendMail({
  277.         $header $this->getTEXTHeader();
  278.         $recip $this->getRecip();
  279.  
  280.         // メール送信
  281.         if (is_array($recip)) {
  282.             foreach ($recip as $rcpt_to{
  283.                 $results[$this->objMail->send($rcpt_to$header$this->body);
  284.             }
  285.         else {
  286.             $results[$this->objMail->send($recip$header$this->body);
  287.         }
  288.  
  289.         $ret true;
  290.         foreach ($results as $result{
  291.             if (PEAR::isError($result)) {
  292.                 GC_Utils_Ex::gfPrintLog($result->getMessage());
  293.                 GC_Utils_Ex::gfDebugLog($header);
  294.                 $ret false;
  295.             }
  296.         }
  297.         return $ret;
  298.     }
  299.  
  300.     // HTMLメール送信を実行する
  301.     function sendHtmlMail({
  302.         $header $this->getHTMLHeader();
  303.         // メール送信
  304.         $result $this->objMail->send($this->getRecip()$header$this->body);
  305.         if (PEAR::isError($result)) {
  306.             GC_Utils_Ex::gfPrintLog($result->getMessage());
  307.             GC_Utils_Ex::gfDebugLog($header);
  308.             return false;
  309.         }
  310.         return true;
  311.     }
  312.  
  313.     /**
  314.      * メーラーバックエンドに応じたパラメータを返す.
  315.      *
  316.      * @param string $backend Pear::Mail のバックエンド
  317.      * @return array メーラーバックエンドに応じたパラメータの配列
  318.      */
  319.     function getBackendParams($backend{
  320.         switch ($backend{
  321.         case "mail":
  322.             $arrParams array();
  323.             break;
  324.         case "sendmail":
  325.             $arrParams array('sendmail_path' => '/usr/bin/sendmail',
  326.                                'sendmail_args' => '-i'
  327.                                );
  328.             break;
  329.         case "smtp":
  330.         default:
  331.             $arrParams array(
  332.                                'host' => $this->host,
  333.                                'port' => $this->port
  334.                                );
  335.             break;
  336.         }
  337.         return $arrParams;
  338.     }
  339. }
  340. ?>

Documentation generated on Fri, 24 Feb 2012 14:00:21 +0900 by Seasoft